package com.xncoding.freemarker.annotation;

import com.xncoding.freemarker.model.DownloadResult;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

@ControllerAdvice
public class DownloadResponseBodyAdvice implements ResponseBodyAdvice<DownloadResult> {

    //实现下载操作
    @Override
    public DownloadResult beforeBodyWrite(DownloadResult result, MethodParameter methodParameter, MediaType mediaType, Class aClass,
                                          ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {
        return result;
    }

    //判断是否有DownloadAble注解进行拦截
    @Override
    public boolean supports(MethodParameter methodParameter, Class aClass) {
        DownloadAble methodAnnotation = methodParameter.getMethodAnnotation(DownloadAble.class);
        if (methodAnnotation == null) {
            return false;
        }
        return true;
    }
}

